package com.util.sqlutil;

import java.util.List;

import static com.util.assertUtil.AssertUtil.AssertNotNull;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/1/28 21:03
 */
public class SqlUtil {

    /**
     * 将列表数据转换为可生成多行数据的可执行sql语句
     * 你应该保证 T为基础类型（或其包装类型)
     * Thread safety
     * 似乎没必要
     */
    public static <T>String listToSQLRow(List<T> data, String columnName) {


        return null;
    }

    /**
     * 将列表数据转换为生成多列数据的可执行sql语句
     * 你应该保证 T为基础类型（或其包装类型)
     * Thread safety
     */
    public static <T>String listToSQLColumn(List<T> data, String columnName) {
        AssertNotNull(data);
        String quot = "";
        if (!(data.get(0) instanceof  Number)) {
            quot = "\'";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("select " + quot + data.get(0) + quot + " as " + columnName);
        for (int i = 1; i < data.size(); i++) {
            sb.append(" union select " + quot + data.get(i) + quot + " as " + columnName);
        }

        return sb.toString();
    }

    /**
     * List中的元素必须为 时间类型的字符串
     * @param data
     * @param columnName
     * @param <T>
     * @return
     */
    public static <T>String listToSQLDateColumn(List<T> data, String columnName) {
        AssertNotNull(data);
        String quot = "";
        if (!(data.get(0) instanceof  Number)) {
            quot = "\'";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("select date(" + quot + data.get(0) + quot + ") as " + columnName);
        for (int i = 1; i < data.size(); i++) {
            sb.append(" union select date(" + quot + data.get(i) + quot + ") as " + columnName);
        }

        return sb.toString();
    }


}
